home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Windows Selection / Windows Selection 1.iso / Graphics / Formula Graphics Multimedia System / STRINGS.SXT < prev    next >
Encoding:
Text File  |  1996-03-30  |  4.3 KB  |  142 lines

  1. //////////////////////////
  2. // Harrow Software 1996
  3. // String examples
  4.  
  5. ////////////////////////////
  6. // (1) Assigning strings
  7.  
  8. $my_string = "This is a string"        // allocate a string
  9. message $my_string                    // display the string
  10.  
  11. $my_string = "Another string"        // reallocate the string
  12. message $my_string
  13.  
  14. // Append another string to the end
  15. $my_string = $my_string, " and another string", CRLF
  16. message $my_string
  17.  
  18. ////////////////////////////
  19. // (2) String operators
  20.  
  21. message "The string length is ", (strlen $my_string), " or ", my_string(0)-1
  22. message "The first 10 characters are '", $my_string strcnt 10, "'"
  23. message "The 5th to 15th characters are '", $my_string stroff 5 strcnt 10, "'"
  24. message "The value of \"10\" is ", strval "10"
  25.  
  26. ////////////////////////////
  27. // (3) Strings as arrays
  28.  
  29. // A string can be considered to be an array of characters
  30. // my_string(0) gets the number of elements in the 1st dimension
  31.  
  32. $my_string = "test"
  33. for n = 0 to my_string(0)-2    // count each character
  34.     message my_string[n], " = '", strchr my_string[n], "'"    // display each character
  35.  
  36. message ""    // next line
  37.  
  38. ///////////////////////////
  39. // (4) Multidimensional string arrays
  40.  
  41. // We can allocate a byte array big enough to store 5 strings
  42. // then we can fill the array with strings
  43.  
  44. my_string_array = new byte[5][32] // each string can have up to 32 characters
  45. $my_string_array[0] = "String 1"        // we can assign a string
  46. $my_string_array[1] = "String 2";"String 3";"String 4";"String 5"
  47.     // we can assign a series of consecutive strings
  48.  
  49. for n = 0 to 4        // we can get each string and display it
  50.     message $my_string_array[n]
  51.  
  52. message ""    // next line
  53.  
  54. ////////////////////////////
  55. // (5) String comparisons
  56.  
  57. // We can compare two strings
  58.  
  59. $string_1 = "Test"
  60. $string_2 = "test"
  61.  
  62. if $string_1 == $string_2 then message "The strings are exactly the same"
  63. if $string_1 ~= $string_2 then message "The strings are the same without case"
  64. if $string_1 != $string_2 then message "The strings are not exactly the same"
  65. if $string_1 !~ $string_2 then message "The strings are not the same without case"
  66.  
  67. message ""    // next line
  68.  
  69. ////////////////////////////
  70. // (6) Switching strings
  71.  
  72. switch $string_1    // case insensitive compare
  73.     case "test"
  74.         message "first choice"
  75.     case "Hello"
  76.         message "second choice"
  77.     default
  78.         message "not found"
  79.  
  80. //////////////////////
  81. // (7) - Text files
  82.  
  83. // A text file can be loaded from disk into a byte array and
  84. //  assigned to a handle. A text file can also be saved to disk
  85.  
  86. load "example.txt" byte my_text
  87. save "example.txt" byte my_text
  88.  
  89. // The following example uses a text file to keep track of
  90. // the number of times an event has been carried out.
  91. // It records the number of hits, and the time and date
  92. // of the last hit.
  93.  
  94. my_hits = 0
  95. if file_exist "log.txt"                // if the log exists
  96.     load "log.txt" byte my_log            // read the log file
  97.     my_hits = strval $my_log stroff 16        // extract the number of hits
  98.  
  99. delete "log.txt"                    // delete the old log
  100. save "log.txt" string "Number of hits: ", my_hits+1    // increment the number
  101. save "log.txt" string CRLF
  102. save "log.txt" string "Last hit: ",TIME," on ",DATE    // save the time and date
  103.  
  104. //////////////////////
  105. // (8) Text databases
  106.  
  107. // You can search the contents of a text array for any
  108. //  matching string. The search will start from the given
  109. //  position in the array
  110.  
  111. my_pos = 0
  112. my_pos = parse my_text[my_pos] for "some string"
  113. if my_pos == ERROR
  114.     message "String not found"
  115.  
  116. // A text array can be broken down into words and numbers. Each
  117. //  word and number in this file will be displayed in the result
  118. //  window
  119.  
  120. my_pos = 0
  121. while my_pos != ERROR
  122.     my_pos = parse my_text[my_pos] to $my_string
  123.     if my_pos != ERROR then message $my_string
  124. message ""
  125.  
  126. // Suppose you wish to load a text file containing a list of
  127. // file names where each file name is on its own new line.
  128. // You want to separated the file names and store them in
  129. // a string array.
  130.  
  131. load "example.txt" byte my_names        // load the text file
  132. my_filenames = new byte[100][16]        // space for up to 100 names
  133.  
  134. pos = 0
  135. for n = 0 to 99
  136.     pos = parse my_names[pos] delimiter CRLF to $buffer
  137.     if pos == ERROR then break
  138.     $my_filenames[n] = $buffer
  139.     message $my_filenames[n]
  140.  
  141. message "the end"
  142.